package com.jnc.web.biz.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import com.jnc.common.constant.SysConstant;
import com.jnc.core.base.model.Menu;
import com.jnc.core.base.model.User;
import com.jnc.core.base.service.MenuService;
import com.jnc.core.util.TreeUtil;
import com.jnc.web.biz.system.mapper.MenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @Author: jjn
 * @Date: 2018/9/20
 * @Desc:
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Menu> queryMenu(Menu menu) {
        return menuMapper.queryMenu(menu);
    }

    @Override
    public List<Menu> checkMenu(Menu record) {
        return menuMapper.checkMenu(record);
    }

    @Override
    public Menu getMenu(Integer menuId) {
        return menuMapper.getMenu(menuId);
    }

    @Transactional(value = SysConstant.TX_BASE_NAME)
    @Override
    public int deleteMenu(Integer menuId) {
        Integer count = menuMapper.deleteMenu(menuId);
        if(count > 0){
            menuMapper.deleteRoleMmenu(menuId);
        }
        return count;
    }

    @Transactional(value = SysConstant.TX_BASE_NAME)
    @Override
    public int addMenu(Menu record) {
        return menuMapper.addMenu(record);
    }

    @Transactional(value = SysConstant.TX_BASE_NAME)
    @Override
    public int editMenu(Menu record) {
        return menuMapper.editMenu(record);
    }

    @Override
    public List<Menu> queryMenusByUser(User user) {
        List<Menu> list = CollUtil.newLinkedList();
        if(user == null){
            return null;
        }
        if(user.isAdmin()){
            list = menuMapper.queryAllNormalMenu();
        }else{
            list = menuMapper.queryMenusByUser(user.getUserId());
        }
        return TreeUtil.getChildMenus(list, 0);
    }

    @Override
    public List<Menu> queryAllMenu() {
        return menuMapper.queryAllMenu();
    }

    @Override
    public List<Menu> queryAllNormalMenu() {
        return menuMapper.queryAllNormalMenu();
    }

    @Override
    public Integer getChildrenMenuCount(Integer parentId) {
        return menuMapper.getChildrenMenuCount(parentId);
    }

    @Override
    public List<String> queryPermsByUser(Integer userId) {
        return menuMapper.queryPermsByUser(userId);
    }

    /**
     * 菜单树列表
     * @return
     */
    @Override
    public List<Map<String, Object>> menuTreeData() {
        List<Map<String, Object>> trees = CollUtil.newArrayList();
        List<Menu> menuList = menuMapper.queryAllMenu();
        trees = getTress(menuList, false, null, false);
        return trees;
    }

    @Override
    public List<Map<String, Object>> roleMenuTreeData(Integer roleId) {
        List<Map<String, Object>> trees = CollUtil.newArrayList();
        List<Menu> menuList = menuMapper.queryAllMenu();
        if(roleId != null){
            List<String> roleMenuList = menuMapper.selectMenuTree(roleId);
            trees = getTress(menuList, true, roleMenuList, true);
        }else{
            trees = getTress(menuList, false, null, true);
        }
        return trees;
    }

    /**
     * 对象转菜单树
     * @param menuList     菜单列表
     * @param isCheck      是否需要选中
     * @param roleMenuList 角色已存在菜单列表
     * @param permsFlag    是否需要显示权限标识
     * @return
     */
    public List<Map<String, Object>> getTress(List<Menu> menuList, boolean isCheck, List<String> roleMenuList, boolean permsFlag){
        List<Map<String, Object>> trees = CollUtil.newArrayList();
        for (Menu menu : menuList){
            Map<String, Object> map = MapUtil.newHashMap();
            map.put("id", menu.getMenuId());
            map.put("pId", menu.getParentId());
            map.put("name", transMenuName(menu, roleMenuList, permsFlag));
            map.put("title", menu.getMenuName());
            if(isCheck){
                map.put("checked", roleMenuList.contains(menu.getMenuId() + menu.getPermission()));
            }else{
                map.put("checked", false);
            }
            trees.add(map);
        }
        return trees;
    }


    private String transMenuName(Menu menu, List<String> roleMenuList, boolean permsFlag){
        StringBuffer sb = new StringBuffer(200);
        sb.append(menu.getMenuName());
        if(permsFlag){
            sb.append("<font color=\"#888\">&nbsp;&nbsp;&nbsp;" + menu.getPermission() + "</font>");
        }
        return sb.toString();
    }
}
